package com.libaba.controller;

import DbToExcel.Main;
import POJO.DbMoudle;
import POJO.HtmlResponseError;

import POJO.ResponseExcel;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


@RequestMapping("/DbToExcel")
@Component("DbToExcelController")
@RestController
public class DbToExcelController {

    //Logger logger = LoggerFactory.getLogger(DbToExcelController.class);


    @RequestMapping(value = "/getInfo")
    public Object GetInfo(DbMoudle dbMoudle) throws IOException {

        System.out.println(dbMoudle);

        try {
            System.out.println("in main start");
            new Main().START(dbMoudle);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("in Error");
            HtmlResponseError error = new HtmlResponseError();
            error.setResult("error");
            error.setMsg("导出失败： " + e.getMessage());
            return error;
        }

        ResponseExcel responseExcel = new ResponseExcel();
        responseExcel.setMsg("导出成功!");
        responseExcel.setEntity(getEntity(dbMoudle));
        return responseExcel;
    }

    public ResponseEntity<byte[]> getEntity(DbMoudle dbMoudle) throws IOException {
        /**
         * 返回导出后的excel
         */
        //下载文件的路径(这里绝对路径)
        String filepath= this.getFilePath(dbMoudle);
        File file =new File(filepath);
        //创建字节输入流，这里不实用Buffer类
        InputStream in = new FileInputStream(file);
        //available:获取输入流所读取的文件的最大字节数
        byte[] body = new byte[in.available()];
        //把字节读取到数组中
        in.read(body);
        //设置请求头
        MultiValueMap<String, String> headers = new HttpHeaders();
        String collectionName = dbMoudle.getCollection();
        headers.add("filename", collectionName);
        //设置响应状态
        HttpStatus statusCode = HttpStatus.OK;
        in.close();
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
        return entity;//返回
    }

    public String getFilePath(DbMoudle dbMoudle) throws IOException {
        /**
         * 获取文件的存储路径
         */
        // 将配置文件加载到流中
        InputStream in = Main.class.getClassLoader().getResourceAsStream("myProperties.properties");
        // 创建并加载配置文件
        Properties pro = new Properties();
        pro.load(in);
        String basePath = pro.getProperty("base_out_put_path");
        String finalPath = basePath + dbMoudle.getCollection() + "/" + dbMoudle.getCollection() + ".xlsx";
        return finalPath;
    }
}
